package com.oaker.hours.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oaker.common.utils.SecurityUtils;
import com.oaker.hours.doman.columns.Columns;
import com.oaker.hours.doman.entity.MhUserLeave;
import com.oaker.hours.mapper.MhUserLeaveMapper;
import com.oaker.hours.service.MhUserLeaveService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * @Description :
 * <功能详细描述>
 * @author: 须尽欢_____
 * @Data : 2021/11/8 16:44
 */
@Service
public class MhUserLeaveServiceImpl extends ServiceImpl<MhUserLeaveMapper, MhUserLeave> implements MhUserLeaveService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(LocalDate leaveDate) {
        Long userId = SecurityUtils.getUserId();
        EntityWrapper<MhUserLeave> wrapper = new EntityWrapper<>();
        wrapper.eq(Columns.MhUserLeave.userId, userId)
                .and()
                .eq(Columns.MhUserLeave.leaveDate, leaveDate);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            return 0L;
        }
        MhUserLeave leave = new MhUserLeave();
        leave.setLeaveDate(leaveDate)
                .setUserId(userId)
                .setCreateTime(new Date());
         baseMapper.insert(leave);
        return leave.getId();
    }

    @Override
    public List<MhUserLeave> queryLeave(Long userId, LocalDate startDate, LocalDate endDate) {
        EntityWrapper<MhUserLeave> wrapper = new EntityWrapper<>();
        wrapper.eq(Columns.MhUserLeave.userId, userId)
                .and()
                .between(Columns.MhUserLeave.leaveDate, startDate, endDate);
        return baseMapper.selectList(wrapper);
    }
}
